<?php

/**
 * 作者：lucas
 * 链接：
 * 邮箱：dissipator_520@qq.com
 */

/**
 * NodeAction
 *
 * @author lucas
 */
class ItemdataAction extends BaseAction {

    public function index() {
        $this->display();
    }

    public function getData() {

        $Mode = M('物料编码');
        $data = $Mode->where($this->condition)->page($this->page, $this->rows)->order($this->order)->select();
        // Log::write($Mode->getLastSql());
        $this->returnGridData($data, $Mode->where($this->condition)->count());

    }

    public function toAdd() {


        $field=array('物料名称','itemno','单价','规格','等级','单位','标准编盘','封装','UK箱','HK箱','SK箱','HWroll');

        foreach ($field as $key => $value) {
            $output .= '<tr><td>' .$value .'：</td><td><input name="'.$value.'" required="required" type="text" value="" style="width: 150px;"/></td></tr>';
            $data['output']= $output;
            // echo  $output;
            $this->assign('data',$data);
        }
        $this->display('add');
    }


    public function doSave() {

        $Model = M("物料编码");
        $data=$_POST;
//	p($data);
	if($data['ID']){
		$Model->data($data)->where('ID= %d',$data['ID'])->save();
		$result =true;
	}
	else{

		if (!$Model->create()) {
        	    $this->returnStatus(false, $Model->getError());
	        } else {
	       	   	$Model->startTrans();
       		    	$result = false;
           	 	if (empty($Model->ID)) {
              	  	$result = $Model->data($data)->add();
        	    	}
		} 
        }     
        $Model->commit();
        $this->returnStatus();
    }


    public function doDelete() {
        $ids = $_POST['ids'];

        $array = explode(',', $ids);
        // p($array);
        if ($array && count($array) > 0) {
            $Model = M('物料编码');
            foreach ($array as $id) {
                $Model->where('ID = %d', $id)->delete();
            }
        }

        $this->returnStatus();
    }


    public function toUpdate() {
        $ID = $_GET['ID'];
        //p($_GET);i
	$field=array('ID','物料名称','itemno','单价','规格','等级','单位','标准编盘','封装','UK箱','HK箱','SK箱','HWroll');
        if ($ID) {
            $Model = M('物料编码');
            $data = $Model->where('ID = %d', $ID)->field($field)->find();
            // p($data);
            if ($data) {
                foreach ($data as $key => $value) {
                    switch ($key)
                    {
                    case 'ID':
                        $output .= '<input name="ID" required="required" type="hidden" value="' .$value. '" style="width: 150px;"/>';

                      break;  
                    case 'label2':
                      $output .= '<tr><td>' .$key .'：</td><td><input name="'.$key.'" required="required" type="text" value="' .$value. '" style="width: 150px;"/></td></tr>';
                      break;
                    default:
                    $output .= '<tr><td>' .$key .'：</td><td><input name="'.$key.'" required="required" type="text" value="' .$value. '" style="width: 150px;"/></td></tr>';
                    }
                    $data['output']=$output ;
                }
                $this->assign('data', $data);
            }
            else 
            $this->assign('data', '无效ID号请检查！');

        }
        $this->display('add');
    }

}

?>
